const axios = require('axios')

axios.defaults.withCredentials = true
// 创建axios实例
const request = axios.create({
  // baseURL: "https://qianniu.bingdou.site",
  timeout: 100000 // 请求超时时间
})

// axios.defaults.baseURL = baseURL;
// request拦截器
request.interceptors.request.use(config => {
  config.data = config.data || {}

  if (!config.data.token) {
    const token = localStorage.getItem('token') || '78f189c9-c9ab-4cc4-83d3-455f744dd08f'
    if (!token) {
      location.href = '/login'
    }
    config.data.token = token
  }
  return config
}, error => {
  return Promise.reject(error)
})

// respone拦截器
request.interceptors.response.use(
  response => {
    /**
         * code为非200是抛错
         */
    const { data, data: { code }} = response
    if (!code) {
      return Promise.reject(response.data || response)
    }
    if (code !== 200) {
      return Promise.reject(data)
    }
    return Promise.resolve(data)
  },
  error => {
    console.log('error:', error)
    return Promise.reject(error)
  }
)

export default request
export { request }
